<template>
  <div class="generate-email-container">
    <div class="container">
      <h1 class="card-title">邮箱生成器</h1>

      <el-form :model="form" label-width="100px" class="email-form">
        <div class="select-container">
          <label style="margin-bottom: 10px" for="emailSuffix">邮箱后缀:</label>
          <el-select
            v-model="form.emailSuffix"
            placeholder="请选择邮箱后缀"
            class="form-control"
            size="large"
          >
            <el-option
              v-for="domain in emailDomains"
              :key="domain"
              :label="domain"
              :value="domain"
            />
          </el-select>
        </div>

        <div class="select-container">
          <label style="margin-bottom: 10px" for="connector">连接符号:</label>
          <el-select
            v-model="form.connector"
            placeholder="请选择连接符号"
            class="form-control"
            size="large"
          >
            <el-option label="点 (.)" value="." />
            <el-option label="下划线 (_)" value="_" />
            <el-option label="横线 (-)" value="-" />
            <el-option label="无连接符" value="" />
          </el-select>
        </div>

        <el-button type="primary" @click="generateEmails" class="btn" size="large">
          生成邮箱
        </el-button>

        <div class="result" :class="{ 'has-content': generatedEmail }">
          <div class="result-text">
            {{ generatedEmail || "点击按钮生成邮箱..." }}
          </div>
          <el-button
            v-if="generatedEmail"
            @click="copyEmail"
            :type="copyButtonType"
            :loading="copying"
            class="copy-btn"
            size="small"
          >
            {{ copyButtonText }}
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus"
import Mock from "mockjs"

// 邮箱后缀数组
const emailDomains = ["momomomowei.icu", "momomomowei.shop"]

// 表单数据
const form = reactive({
  emailSuffix: emailDomains[0],
  connector: "."
})

// 生成的邮箱
const generatedEmail = ref("")

// 复制按钮状态
const copying = ref(false)
const copyButtonText = ref("复制")
const copyButtonType = ref("success")

// 生成邮箱
const generateEmails = () => {
  const suffix = form.emailSuffix || emailDomains[0]
  const connector = form.connector

  const data = Mock.mock({
    email: function () {
      const firstName = Mock.mock("@first").toLowerCase()
      const lastName = Mock.mock("@last").toLowerCase()
      return `${firstName}${connector}${lastName}@${suffix}`
    }
  })

  generatedEmail.value = data.email
}

// 复制邮箱
const copyEmail = async () => {
  if (!generatedEmail.value) return

  copying.value = true

  try {
    await navigator.clipboard.writeText(generatedEmail.value)
    copyButtonText.value = "已复制!"
    copyButtonType.value = "info"
    ElMessage.success("邮箱地址已复制到剪贴板")

    setTimeout(() => {
      copyButtonText.value = "复制"
      copyButtonType.value = "success"
    }, 2000)
  } catch (error) {
    // 如果现代API不可用，使用传统方法
    try {
      const textArea = document.createElement("textarea")
      textArea.value = generatedEmail.value
      document.body.appendChild(textArea)
      textArea.select()
      document.execCommand("copy")
      document.body.removeChild(textArea)

      copyButtonText.value = "已复制!"
      copyButtonType.value = "info"
      ElMessage.success("邮箱地址已复制到剪贴板")

      setTimeout(() => {
        copyButtonText.value = "复制"
        copyButtonType.value = "success"
      }, 2000)
    } catch (fallbackError) {
      ElMessage.error("复制失败，请手动复制")
    }
  } finally {
    copying.value = false
  }
}
</script>

<style lang="scss" scoped>
.generate-email-container {
  font-family: Arial, sans-serif;
  padding: 30px;
  margin: 0;
  min-height: 100%;
}

// .container {
//   background: white;
//   padding: 30px;
//   max-width: 600px;
//   width: 100%;
//   border-radius: 8px;
//   box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
// }

.card-title {
  text-align: center;
  margin-bottom: 25px;
  font-size: 1.5rem;
  color: #333;
  font-weight: 600;
}

.select-container {
  position: relative;
  margin-bottom: 15px;

  label {
    display: block;
    margin-bottom: 5px;
    font-weight: 500;
    color: #333;
    font-size: 1rem;
  }
}

.form-control {
  width: 100%;

  :deep(.el-input__wrapper) {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 1rem;
    background-color: white;
    transition: all 0.3s ease;

    &:hover {
      border-color: #007bff;
    }

    &.is-focus {
      border-color: #007bff;
      outline: none;
    }
  }

  :deep(.el-input__inner) {
    font-size: 1rem;
    padding: 0;
  }

  :deep(.el-select .el-input__suffix) {
    right: 10px;
  }
}

.btn {
  margin-top: 10px;
  width: 100%;
  background: #007bff;
  color: white;
  border: none;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  margin-bottom: 15px;
  transition: background-color 0.3s ease;

  &:hover {
    background: #0056b3;
  }

  &:focus {
    outline: none;
  }
}

.result {
  background: #f8f9fa;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 50px;
  margin-top: 15px;
}

.result-text {
  flex: 1;
  font-family: monospace;
  font-size: 1.2rem;
  font-weight: bold;
  color: #333;
}

.copy-btn {
  background: #28a745;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  margin-left: 10px;
  transition: background-color 0.3s ease;

  &:hover {
    background: #218838;
  }

  &.el-button--info {
    background: #17a2b8;

    &:hover {
      background: #138496;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .generate-email-container {
    padding: 20px 15px;
  }

  .container {
    padding: 25px 20px;
  }

  .card-title {
    font-size: 1.3rem;
  }
}

@media (max-width: 480px) {
  .generate-email-container {
    padding: 15px 10px;
  }

  .container {
    padding: 20px 15px;
  }

  .card-title {
    font-size: 1.2rem;
  }

  .result-text {
    font-size: 1rem;
  }
}
</style>
